package com.anpy.user.controller;

import com.anpy.common.service.model.result.ApiResult;
import com.anpy.common.service.model.vo.ResponseMessage;
import com.anpy.common.service.model.vo.Result;
import com.anpy.user.entity.LoginRequestVO;
import com.anpy.user.entity.UserEntity;
import com.anpy.user.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@MapperScan(value = "com.anpy.user.mapper")
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理接口")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public ApiResult<UserEntity> login(@RequestBody @ApiParam("登录请求") LoginRequestVO loginRequestVO){
        UserEntity userEntity = userService.login(loginRequestVO);
        if (userEntity == null) {
            return ApiResult.failed("账号或密码错误");
        }
        return ApiResult.success(userEntity);
    }

    @PostMapping("/save")
    @ApiOperation("保存用户信息")
    public ResponseMessage<Boolean> saveUserInfo(@RequestBody @ApiParam("用户信息") UserEntity userEntity){
        return Result.success(userService.saveUserInfo(userEntity));
    }


    @PostMapping("/update")
    @ApiOperation("更新用户信息")
    public ResponseMessage<Boolean> uploadUserInfo(@RequestBody @ApiParam("用户信息") UserEntity userEntity){
        return Result.success(userService.uploadUserInfo(userEntity));
    }

    @GetMapping("/load")
    @ApiOperation("获取用户信息")
    public ResponseMessage<UserEntity> loadUserInfo(@ApiParam("用户ID") Integer userId){
        return Result.success(userService.loadUserInfo(userId));
    }
    
    @GetMapping("/cache")
    @ApiOperation("获取缓存的用户数据")
    public ResponseMessage<UserEntity> cache(@ApiParam("手机号") String phone){
        return Result.success(userService.loadCacheData(phone));
    }

    @GetMapping("/test")
    @ApiOperation("测试接口")
    public ResponseMessage<UserEntity> test(){
        return Result.success();
    }
    
    /**
     * 获取所有用户列表
     * @return 用户列表
     */
    @GetMapping("/list")
    @ApiOperation("获取所有用户列表")
    public ResponseMessage<List<UserEntity>> getAllUsers() {
        return Result.success(userService.getAllUsers());
    }
    
    /**
     * 分页查询用户列表
     * @param page 页码
     * @param size 每页大小
     * @param userName 用户名（可选，用于模糊查询）
     * @param phone 手机号（可选，用于模糊查询）
     * @return 分页结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询用户列表")
    public ResponseMessage<Page<UserEntity>> getUsersByPage(
            @RequestParam(defaultValue = "1") @ApiParam("页码") int page,
            @RequestParam(defaultValue = "10") @ApiParam("每页大小") int size,
            @RequestParam(required = false) @ApiParam("用户名") String userName,
            @RequestParam(required = false) @ApiParam("手机号") String phone) {
        return Result.success(userService.getUsersByPage(page, size, userName, phone));
    }

}
